package com.fieldchain.ex.fa100.custom.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;

import com.fieldchain.ex.fa100.inter.IntegerCallBack;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/9
 */
public class MySortRadio extends android.support.v7.widget.AppCompatRadioButton{
    private int type=0;
    private int d0,d1,d2;
    public MySortRadio(Context context) {
        super(context);
    }

    public MySortRadio(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setDrawable(int d0,int d1,int d2){
        setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(d0), null);
        this.d1=d1;
        this.d2=d2;
        this.d0=d0;
    }
    public void setListener(final IntegerCallBack callBack){

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(type==2||type==0){
                    //up
                    type=1;
                    setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(d1), null);
                }else  if(type==1||type==0){
                    //down
                    type=2;
                    setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(d2), null);
                }

                callBack.index(type);
            }
        });

        setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(!b){
                   type=0;
                    setCompoundDrawablesWithIntrinsicBounds(null, null, getResources().getDrawable(d0), null);
                }
            }
        });
    }
}
